#include "ReadWarehouse.h"
#include "Translator.h"
#include "BaseTypeDefin.h"
#include "myExceptions.h"

void main(int argc, char* argv[])
{
	
	wcout.imbue( locale( "Russian_Russia.866" ) );
	
	ReadWarehouse* r;
	try
	{
		r = new ReadWarehouse("E:\\projects\\source-C++\\NumberToWord\\dict.txt");
	}
	catch (CantOpenDict) 
	{
		cout << "Can`t open file dict" << endl;
		delete r;
		return;
	}
	catch (exception& e)
	{
		cout << e.what() << endl;
		delete r;
		return;
	}
	catch (...)
	{
		cout << "Error while open and read from file dict" << endl;
		delete r;
		return;
	}

	Translator* t;
	try
	{
		try
		{
			t = new Translator( r->getDictionary() );
		}
		catch (EmptyDictionary) 
		{
			cout << "Dictionary is empty " << endl;
			delete t;
			delete r;
			return;
		}
		catch (exception* e)
		{
			cout << e->what() << endl;
			delete t;
			delete r;
			return;
		}

		string tempDec, tempOct;
		while ( cin >> tempDec )
		{
			if ( tempDec == "exit" )
			{
				break;
			}
			cout << "temp=" << tempDec << endl;

			int decNumber = 0;
			istringstream myStream( tempDec );

			try
			{
				myStream >> decNumber ;
			}
			catch (exception* e)
			{
				cout << e->what() << endl;
				cout << "Please, enter new number " << endl;
				continue;
			}
			catch (...)
			{
				cout << "Please, enter new number " << endl;
				continue;
			}
			stringstream outDec;
			outDec << decNumber;
			tempDec.clear();
			tempDec.assign(outDec.str());

			stringstream outOct;
			outOct << oct << decNumber;
			tempOct.clear();
			tempOct.assign(outOct.str());

			cout << "Decimal version of string variable temp is " << tempDec << endl;
			cout << "Octal version of string variable temp is " <<  tempOct << endl;
			
			t->DoTranslate(tempDec);

			wcout << t->getResult() << endl;;
		}
	}
	catch (...)
	{
		delete t;
		delete r;
		cout << "!!! Fatal Error !!!" << endl;
	}	
	return;
}